package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.BaseConstant;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.base.RoomtypeBaseParamDto;
import com.link2room.ironhide.facade.IRoomtypeFacadeService;
import com.link2room.ironhide.feign.optimus.StaticPriceFeignService;
import com.link2room.ironhide.feign.soundwave.PmsRmtypeFeignService;
import com.link2room.ironhide.service.IHotelRmtypeService;
import com.link2room.optimus.dto.param.base.PriceStaticBaseParamDto;
import com.link2room.soundwave.dto.param.inside.PmsRmtypeInsideParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelRmtypePictureDto;
import com.link2room.ironhide.dto.result.base.L2RRoomtypeDto;
import com.link2room.ironhide.entity.L2RHotelRmtypePicture;
import com.link2room.ironhide.entity.L2RHotelRoomtype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sss on 2016/8/29.
 */
@Component("roomtypeFacadeService")
public class RoomtypeFacadeServiceImpl implements IRoomtypeFacadeService {

    @Resource
    private ICommonService commonService;
    @Autowired
    private PmsRmtypeFeignService pmsRmtypeFeignService;
    @Resource
    private IHotelRmtypeService hotelRmtypeService;
    @Autowired
    private StaticPriceFeignService staticPriceFeignService;


    /**
     * 房型基础信息列表
     * @param roomtypeBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RRoomtypeDto> listRoomType(RoomtypeBaseParamDto roomtypeBaseParamDto)throws Exception{
        QueryResult<L2RHotelRoomtype> roomtypeResult = commonService.findAllEQ(L2RHotelRoomtype.class,ClassConverUtil.copyProperties(roomtypeBaseParamDto,L2RHotelRoomtype.class), roomtypeBaseParamDto.getCurrentPage(), roomtypeBaseParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(roomtypeResult, L2RRoomtypeDto.class, false);
    }

    /**
     * 房型基础信息（含图片）
     * @param roomtypeBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RRoomtypeDto getRoomtypeDtoById(RoomtypeBaseParamDto roomtypeBaseParamDto)throws Exception{
        L2RHotelRoomtype roomtype = commonService.findOneById(L2RHotelRoomtype.class, roomtypeBaseParamDto.getId());
        L2RRoomtypeDto l2RRoomtypeDto=null;
        if(roomtype!=null){
            l2RRoomtypeDto=this.composeRmtypeDto(roomtype);
        }
        return l2RRoomtypeDto;
    }

    /**
     * 房型基础信息（含图片）
     * @param roomtypeBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RRoomtypeDto getRoomtypeDtoByRmtype(RoomtypeBaseParamDto roomtypeBaseParamDto)throws Exception{
        L2RHotelRoomtype queryRmtype=new L2RHotelRoomtype();
        queryRmtype.setLhotelGroupCode(roomtypeBaseParamDto.getLhotelGroupCode());
        queryRmtype.setLhotelCode(roomtypeBaseParamDto.getLhotelCode());
        queryRmtype.setRmtype(roomtypeBaseParamDto.getRmtype());
        L2RHotelRoomtype roomtype = commonService.findOneEQ(L2RHotelRoomtype.class, queryRmtype);
        L2RRoomtypeDto l2RRoomtypeDto=null;
        if(roomtype!=null){
            l2RRoomtypeDto=this.composeRmtypeDto(roomtype);
        }
        return l2RRoomtypeDto;
    }

    /**
     * 组装房型基础信息（含图片）
     * @param roomtype
     * @return
     * @throws Exception
     */
    private L2RRoomtypeDto composeRmtypeDto(L2RHotelRoomtype roomtype) throws Exception {
        L2RRoomtypeDto l2RRoomtypeDto=ClassConverUtil.copyPropertiesToDto(roomtype,L2RRoomtypeDto.class, false);
        L2RHotelRmtypePicture queryHRPIC=new L2RHotelRmtypePicture();
        queryHRPIC.setLhotelGroupCode(roomtype.getLhotelGroupCode());
        queryHRPIC.setLhotelCode(roomtype.getLhotelCode());
        queryHRPIC.setRmtype(roomtype.getRmtype());
        queryHRPIC.setCategroy(BaseConstant.HotelBaseInfoConstant.PICCATEGROY.BIGPIC);
        QueryResult<L2RHotelRmtypePicture> bigPicResult = commonService.findAllEQ(L2RHotelRmtypePicture.class, queryHRPIC);
        if(bigPicResult.getResultlist()!=null&&bigPicResult.getResultlist().size()>0){
            l2RRoomtypeDto.setMainHotelRmtypePic(ClassConverUtil.copyPropertiesToDto(bigPicResult.getResultlist().get(0),L2RHotelRmtypePictureDto.class,false));
            l2RRoomtypeDto.getMainHotelRmtypePic().setId(null);
        }
        queryHRPIC.setCategroy(BaseConstant.HotelBaseInfoConstant.PICCATEGROY.PICS);
        QueryResult<L2RHotelRmtypePicture> picsResult = commonService.findAllEQ(L2RHotelRmtypePicture.class, queryHRPIC);
        if(picsResult.getResultlist()!=null&&picsResult.getResultlist().size()>0) {
            l2RRoomtypeDto.setHotelRmtypePics(ClassConverUtil.copyPropertiesListToDtoList(picsResult.getResultlist(),L2RHotelRmtypePictureDto.class,false));
            for(L2RHotelRmtypePictureDto rmPic:l2RRoomtypeDto.getHotelRmtypePics()){
                rmPic.setId(null);
            }
        }
        return l2RRoomtypeDto;
    }

    /**
     * 房型基础信息
     * @param roomtypeBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RRoomtypeDto getRoomtypeDto(RoomtypeBaseParamDto roomtypeBaseParamDto)throws Exception{
        L2RHotelRoomtype queryLHR=new L2RHotelRoomtype();
        queryLHR.setLhotelGroupCode(roomtypeBaseParamDto.getLhotelGroupCode());
        queryLHR.setLhotelCode(roomtypeBaseParamDto.getLhotelCode());
        queryLHR.setRmtype(roomtypeBaseParamDto.getRmtype());
        L2RHotelRoomtype roomtype = commonService.findOneEQ(L2RHotelRoomtype.class, queryLHR);
        return ClassConverUtil.copyPropertiesToDto(roomtype, L2RRoomtypeDto.class, false);
    }

    /**
     * 保存房型
     * @param roomtypeBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RRoomtypeDto saveOrUpdateRoomtypeDto(RoomtypeBaseParamDto roomtypeBaseParamDto)throws Exception{
        L2RRoomtypeDto rmtypeDto = roomtypeBaseParamDto.getL2RRoomtypeDto();
        L2RHotelRoomtype l2rHotelRoomtype = ClassConverUtil.copyProperties(rmtypeDto, L2RHotelRoomtype.class);
        if(StringUtil.isBlank(l2rHotelRoomtype.getRmtype())){
            l2rHotelRoomtype.setRmtype(l2rHotelRoomtype.getLhotelCode()+ DefaultValue.MARK+l2rHotelRoomtype.getGroupRmtype());
        }
        L2RHotelRmtypePicture bigPic = ClassConverUtil.copyProperties(rmtypeDto.getMainHotelRmtypePic(), L2RHotelRmtypePicture.class);
        List<L2RHotelRmtypePicture> hotelRmtypePics = ClassConverUtil.copyPropertiesList(rmtypeDto.getHotelRmtypePics(), L2RHotelRmtypePicture.class);
        hotelRmtypeService.saveOrUpdateHotelRmtype(l2rHotelRoomtype,bigPic,hotelRmtypePics);
        if(l2rHotelRoomtype.getId()!=null){
            PriceStaticBaseParamDto priceStaticBaseParamDto=new PriceStaticBaseParamDto();
            priceStaticBaseParamDto.setLhotelGroupCode(l2rHotelRoomtype.getLhotelGroupCode());
            priceStaticBaseParamDto.setLhotelCode(l2rHotelRoomtype.getLhotelCode());
            priceStaticBaseParamDto.setRmtype(l2rHotelRoomtype.getRmtype());
            priceStaticBaseParamDto.setIsHalt(l2rHotelRoomtype.getIsHalt());
            staticPriceFeignService.updateStaticPrice(priceStaticBaseParamDto);
        }
        PmsRmtypeInsideParamDto pmsRmtypeInsideParamDto=new PmsRmtypeInsideParamDto();
        pmsRmtypeInsideParamDto.setLhotelCode(l2rHotelRoomtype.getLhotelCode());
        pmsRmtypeInsideParamDto.setPmsRmtype(l2rHotelRoomtype.getGroupRmtype());
        pmsRmtypeInsideParamDto.setL2rRmtype(l2rHotelRoomtype.getRmtype());
        pmsRmtypeInsideParamDto.setL2rRmtypeName(l2rHotelRoomtype.getName());
        pmsRmtypeFeignService.newRmtype(pmsRmtypeInsideParamDto);
        L2RRoomtypeDto l2rRoomtypeDto = ClassConverUtil.copyPropertiesToDto(l2rHotelRoomtype, L2RRoomtypeDto.class, false);
        if(bigPic!=null){
            bigPic.setId(null);
        }
        l2rRoomtypeDto.setMainHotelRmtypePic(ClassConverUtil.copyPropertiesToDto(bigPic,L2RHotelRmtypePictureDto.class,false));
        if(hotelRmtypePics!=null){
            for(L2RHotelRmtypePicture hotelRmtypePic:hotelRmtypePics){
                hotelRmtypePic.setId(null);
            }
        }
        l2rRoomtypeDto.setHotelRmtypePics(ClassConverUtil.copyPropertiesListToDtoList(hotelRmtypePics,L2RHotelRmtypePictureDto.class,false));
        return l2rRoomtypeDto;
    }

    /**
     * 保存房型
     * @param roomtypeBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RRoomtypeDto saveOrUpdateRoomtypeBase(RoomtypeBaseParamDto roomtypeBaseParamDto)throws Exception{
        L2RRoomtypeDto rmtypeDto = roomtypeBaseParamDto.getL2RRoomtypeDto();
        L2RHotelRoomtype l2rHotelRoomtype = ClassConverUtil.copyProperties(rmtypeDto, L2RHotelRoomtype.class);
        if(StringUtil.isBlank(l2rHotelRoomtype.getRmtype())){
            l2rHotelRoomtype.setRmtype(l2rHotelRoomtype.getLhotelCode()+ DefaultValue.MARK+l2rHotelRoomtype.getGroupRmtype());
        }
        commonService.saveOrUpdate(l2rHotelRoomtype);
        if(l2rHotelRoomtype.getId()!=null){
            PriceStaticBaseParamDto priceStaticBaseParamDto=new PriceStaticBaseParamDto();
            priceStaticBaseParamDto.setLhotelGroupCode(l2rHotelRoomtype.getLhotelGroupCode());
            priceStaticBaseParamDto.setLhotelCode(l2rHotelRoomtype.getLhotelCode());
            priceStaticBaseParamDto.setRmtype(l2rHotelRoomtype.getRmtype());
            priceStaticBaseParamDto.setIsHalt(l2rHotelRoomtype.getIsHalt());
            staticPriceFeignService.updateStaticPrice(priceStaticBaseParamDto);
        }
        L2RRoomtypeDto l2rRoomtypeDto = ClassConverUtil.copyPropertiesToDto(l2rHotelRoomtype, L2RRoomtypeDto.class, false);
        return l2rRoomtypeDto;
    }

    @Override
    public QueryResult<L2RHotelRmtypePictureDto> getRmtypePics(RoomtypeBaseParamDto roomtypeBaseParamDto) throws Exception {
        L2RCondition l2RCondition=new L2RCondition();
        l2RCondition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,roomtypeBaseParamDto.getLhotelGroupCode()),
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.EQ,roomtypeBaseParamDto.getLhotelCode()),
                new ConditionDetail(L2RHotelRmtypePicture.L2RHotelRmtypePicture_.rmtype.toString(), CriteriaUtil.Operator.IN,roomtypeBaseParamDto.getRmtypes()),
                new ConditionDetail(L2RHotelRmtypePicture.L2RHotelRmtypePicture_.categroy.toString(), CriteriaUtil.Operator.EQ, BaseConstant.HotelBaseInfoConstant.PICCATEGROY.PICS)
        });
        QueryResult<L2RHotelRmtypePicture> l2rHotelPictureResult = commonService.findAllByCondition(L2RHotelRmtypePicture.class, l2RCondition);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rHotelPictureResult, L2RHotelRmtypePictureDto.class, false);
    }

    @Override
    public QueryResult<L2RHotelRmtypePictureDto> getRmtypeBigPic(RoomtypeBaseParamDto roomtypeBaseParamDto) throws Exception {
        L2RCondition l2RCondition=new L2RCondition();
        l2RCondition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,roomtypeBaseParamDto.getLhotelGroupCode()),
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.EQ,roomtypeBaseParamDto.getLhotelCode()),
                new ConditionDetail(L2RHotelRmtypePicture.L2RHotelRmtypePicture_.rmtype.toString(), CriteriaUtil.Operator.IN,roomtypeBaseParamDto.getRmtypes()),
                new ConditionDetail(L2RHotelRmtypePicture.L2RHotelRmtypePicture_.categroy.toString(), CriteriaUtil.Operator.EQ, BaseConstant.HotelBaseInfoConstant.PICCATEGROY.BIGPIC)
        });
        QueryResult<L2RHotelRmtypePicture> l2rHotelPictureResult = commonService.findAllByCondition(L2RHotelRmtypePicture.class, l2RCondition);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rHotelPictureResult, L2RHotelRmtypePictureDto.class, false);
    }
}
